/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-28 13:49:38
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-07-24 15:13:17
 * @FilePath: \A05\App\task.c
 * @Description: 任务源文件
 */

#include "systick.h"
#include "task.h"
#include "charger.h"
// #include "display.h"
#include "qc_protocol.h"

#define Task_num    10

Task_t Task_list[Task_num];


/**
 * @description: 1ms任务
 * @return {*}
 */
static void _1ms_task(void){
    /*qc诱导9V*/
    qc_protocol_process();

    /*充电器状态机*/
    Charger_state_machine(&charger);

    /*开路电压检测*/
    Open_voltage_detect();

    /*获取C+输入电压*/
    if(charger.vol_samp_flag == 0){
        Get_vin_voltage(&charger);
    }
    

    /*快充检测*/
    flash_charger_detect(&charger);

    if(charger.battery_L.state == BAT_IDLE){
        charger.battery_L.current_stage = CURRENT_05A;
    }

    if(charger.battery_R.state == BAT_IDLE){
        charger.battery_R.current_stage = CURRENT_05A;
    }


    
    /*充电开启/关断*/
    Battery_charge_en(&charger.battery_L);
    Battery_charge_en(&charger.battery_R);
    
    /*温度检测*/
    Temp_protect(&charger);
    charger.battery_L.err.Bit.Temp_Fault = charger.error_OT | charger.error_LT ;
    charger.battery_R.err.Bit.Temp_Fault = charger.error_OT | charger.error_LT ;

    /*电池状态机*/
    Battery_state_machine(&charger.battery_L);
    Battery_state_machine(&charger.battery_R);
}


uint8_t gDisplay_refresh = 0;
extern void Page_disp_process(void);
extern void Temp_control(charger_t * pChg);
extern void Limt_max_temp(charger_t * pChg);
// static uint8_t pid_cnt = 0;
/**
 * @description: 10ms任务
 * @return {*}
 */
static void _10ms_task(void){
    /*电池进程*/
    Battery_process(&charger.battery_L);
    Battery_process(&charger.battery_R);

    // if(++pid_cnt > 10){
    //     pid_cnt = 0;
    //     Temp_control(&charger);
    // }

    Limt_max_temp(&charger);
    
    /*pid*/
    Pid_calculat_process(&charger.battery_L);
    Pid_calculat_process(&charger.battery_R);

    /*显示控制*/
    static uint8_t delay_cnt = 0;
    if(gDisplay_refresh == 1){
        delay_cnt = 0;
    }
    else{
        if(++delay_cnt > 25)        /*250ms刷新一次*/
        {
            delay_cnt = 0;
            gDisplay_refresh = 1;
        }
    }

    /*屏幕显示*/
    if(gDisplay_refresh){
        gDisplay_refresh = 0;
        Page_disp_process();
    }
    
}
    

#define DEBUG
/**
 * @description: 100ms任务
 * @return {*}
 */
static void _100ms_task(void){
    #ifdef DEBUG
    LOGI("battery L voltage: %d mV"      , charger.battery_L.voltage      );
    LOGI("battery L current: %d mA"      , charger.battery_L.current      );
    LOGI("battery L voltage stage : %d"  , charger.battery_L.vol_stage    );
    LOGI("battery L current stage : %d"  , charger.battery_L.current_stage);
    LOGI("battery L current set : %d mA" , charger.battery_L.current_set  );
    LOGI();
    LOGI();
    
    LOGI("battery R voltage: %d mV"      , charger.battery_R.voltage      );
    LOGI("battery R current: %d mA"      , charger.battery_R.current      );
    LOGI("battery R voltage stage : %d"  , charger.battery_R.vol_stage    );
    LOGI("battery R current stage : %d"  , charger.battery_R.current_stage);
    LOGI("battery R current set : %d mA" , charger.battery_R.current_set  );
    LOGI();
    LOGI();
    #endif
}



/**
 * @description: 1s任务
 * @return {*}
 */
static void _1s_task(void){
    /*预充电超时检测*/
    if(charger.battery_L.state == BAT_PreChage){
        PreCharge_timeout_det(&charger.battery_L);
    } else{
        charger.battery_L.pre_charg_time = 0;
    }
    
    if(charger.battery_R.state == BAT_PreChage){
        PreCharge_timeout_det(&charger.battery_R);
    } else{
        charger.battery_R.pre_charg_time = 0;
    }
}


/**
 * @description: 1min任务
 * @return {*}
 */
static void _1min_task(void){
    /*定时关断充电*/
    if(charger.battery_L.state == BAT_Charge){
        Timed_off_charge(&charger.battery_L);
    } else{
        charger.battery_L.charge_time = 0;
    }

    if(charger.battery_R.state == BAT_Charge){
        Timed_off_charge(&charger.battery_R);
    } else{
        charger.battery_R.charge_time = 0;
    }
}



/**
 * @description: 任务初始化
 * @return {*}
 */
void Task_init(void){
    Task_create(&Task_list[0] , _1MS_TICK , _1ms_task);
    Task_create(&Task_list[1] , _10MS_TICK , _10ms_task);
    Task_create(&Task_list[2] , _100MS_TICK , _100ms_task);
    Task_create(&Task_list[3] , _1S_TICK , _1s_task);
    Task_create(&Task_list[4] , _1min_TICK , _1min_task);
}


/**
 * @description: 创建一个任务
 * @param {Task_t} *task
 * @param {uint32_t} delay
 * @return {*}
 */
void Task_create(Task_t *task , uint32_t delay , Func_t func){
    task->exe        = func ;
    task->delay_time = delay;
    task->pre_time   = 0    ;
}



/**
 * @description: 
 * @param {Task_t} *task
 * @return {*}
 */
void Task_run(Task_t *task){
    uint32_t curr_tick = Get_system_Timestamp();
    if (Get_Time_gap(task->pre_time, curr_tick) >= task->delay_time) {
        task->exe();
        task->pre_time = curr_tick;
    }
}


/**
 * @description: 任务调度
 * @return {*}
 */
void Task_scheduler(void){
    for(int i = 0; (i < Task_num) && (Task_list[i].exe != NULL) ; i++){
        Task_run(&Task_list[i]);
    }
}

